﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Office = Microsoft.Office.Core;
using System.Collections.ObjectModel;
using log4net;
using System.ComponentModel;
using ThreeByte.ActiveDeck.Content;
using ThreeByte.ActiveDeck.AddIn.Content.Controls;

namespace ThreeByte.ActiveDeck.AddIn.Controls.Wizard.Pages
{
    /// <summary>
    /// Interaction logic for SelectVideoPage.xaml
    /// </summary>
    public partial class SelectVideoPage : Page, INotifyPropertyChanged
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(SelectVideoPage));
        private CoreConfig Config;

        public SelectVideoPage() {
            InitializeComponent();

            Config = WizardMainFrame.ActiveConfig;
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName) {
            if(PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
     

        private void NextButton_Click(object sender, RoutedEventArgs e) {
           
        }

        private void CannotExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = false;
            e.Handled = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e) {

        }

        private void SelectVideo_Click(object sender, RoutedEventArgs e) {
            //Open a file browser dialog
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.CheckFileExists = true;
            fileDialog.Filter = "Video Files (*.mov)|*.mov|All files (*.*)|*.*";
            System.Windows.Forms.DialogResult result = fileDialog.ShowDialog();
            if(result != System.Windows.Forms.DialogResult.OK) {
                return;
            }

            string filePath = fileDialog.FileName;
            string fileName = fileDialog.SafeFileName;
            string pathRoot = Config.GetPresentationPath();

            DesignerSlideConfig portraitConfig = new DesignerSlideConfig();
            portraitConfig.Elements.Add(new DesignerVideo(filePath, pathRoot, new Media.PixelSpace() { X = 0, Y = 0, H = 1020, W = 768 }) { LayoutOrientation = ThreeByte.ActiveDeck.Content.Orientation.Portrait });
            portraitConfig.Name = string.Format("Video_{0}", fileName);
            PortraitButton.Tag = portraitConfig;
            PortraitPreviewRoot.Content = PagePreviewFactory.Create(portraitConfig);

            DesignerSlideConfig landscapeConfig = new DesignerSlideConfig();
            landscapeConfig.Elements.Add(new DesignerVideo(filePath, pathRoot, new Media.PixelSpace() { X = 0, Y = 0, H = 1020, W = 768 }) { LayoutOrientation = ThreeByte.ActiveDeck.Content.Orientation.LandscapeLeft });
            landscapeConfig.Name = string.Format("Video_{0}", fileName);
            LandscapeButton.Tag = landscapeConfig;
            LandscapePreviewRoot.Content = PagePreviewFactory.Create(landscapeConfig);

            FileNameTextBox.Text = System.IO.Path.GetFileName(fileName);
        }

        private void ChooseVideo_Click(object sender, RoutedEventArgs e) {
            Button btn = (Button)sender;
            bool landscape = (btn == LandscapeButton);
            PageConfig config = (PageConfig)(btn.Tag);
            if(config != null) {
                NavigationService navService = this.NavigationService;
                if(navService != null) {
                    navService.Navigate(new VideoAssignmentPage(config, landscape));
                }
            }
        }        

        
    }
}
